package cn.quevo.client.jssc;

import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;

public class SerialRecieveData {

    public static void main(String[] args) throws InterruptedException {
        new Thread(new Runnable() {                                                            //开启线程
            @Override
            public void run() {
                SerialPort serialPort = new SerialPort("COM3");     //设置串口
                try {
                    //Open port
                    serialPort.openPort();
                    //We expose the settings. You can also use this line - serialPort.setParams(9600, 8, 1, 0);
                    serialPort.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
//                    String buffer = serialPort.readString(20);                  //从串口读取5字符字符串
//                    System.out.println(buffer);
                    /*if (buffer.trim().equals("Hello")) {
                        System.out.println("start write");
                        serialPort.writeBytes("1".getBytes());                 // 往串口写入数据
                    } else {
                        System.out.println("failed");
                    }*/
//                    serialPort.writeBytes("@0008010000DE".getBytes());
                    System.out.println(serialPort.writeString("@0008010000DE"));
                    Thread.sleep(400);
                    System.out.println(serialPort.readBytes());
                    //Closing the port
//                    serialPort.closePort();

                } catch (SerialPortException ex) {
                    ex.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

}
